﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Data.SqlClient;
using System.IO;
using RawData;
using SqlShareAPI;
using System.Data;



namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            TestFileUpload();
        }

        static void TestFileUpload()
        {
            string tblname = "test's upload";
            SqlConnection conn = new SqlConnection("Server=tcp:ec2-184-72-5-110.us-west-1.compute.amazonaws.com;User ID=meta_test;Password=eScience4All!;Trusted_Connection=False;Database = sqlshare.v2.content;");
            conn.Open();

            SqlCommand cmd = new SqlCommand("IF(EXISTS(SELECT * FROM sys.tables WHERE name = @tblname)) DROP TABLE [ " + tblname + " ]", conn);
            cmd.Parameters.Add("@tblname", SqlDbType.VarChar).Value = tblname;
            cmd.ExecuteNonQuery();

            Stream stream = new FileInfo(@"C:\Users\gbc3\Documents\test_upload1.txt").OpenRead();
            DataFileReader reader = new DataFileReader(tblname, '\t', true);
            Console.WriteLine("reading..");
            reader.ExtractMetadata(stream, false);
            Console.WriteLine(reader.SQLTable.SQLCreateTable);            
            SqlBatchFileUploader loader = new SqlBatchFileUploader(reader.SQLTable, 4000, true);
            loader.BulkCopyFile(stream, conn, false);
            conn.Close();
        }

        static void CreateAdminAccount(string username, string pasword, string fullname)
        {
            string contentStr = "Server=tcp:ec2-184-72-5-110.us-west-1.compute.amazonaws.com;User ID=web_user;Password=eScience4All!;Trusted_Connection=False;Database = SqlShare.v2.content;";
            string metaStr = "Server=tcp:ec2-184-72-5-110.us-west-1.compute.amazonaws.com;User ID=web_user;Password=eScience4All!;Trusted_Connection=False;Database = SqlShare.v2.meta;";

            string passEnc = SSAuthentication.DESEncrypt(username + pasword);

            SqlConnection conn;
            SqlCommand cmd;

            conn = new SqlConnection(metaStr);
            
            conn.Open();
            cmd = new SqlCommand("sp_user_create", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = passEnc;
            cmd.Parameters.Add("@fullname", SqlDbType.VarChar).Value = fullname;
            cmd.Parameters.Add("@is_admin", SqlDbType.Bit).Value = true;
            cmd.ExecuteNonQuery();
            conn.Close();
            
            conn = new SqlConnection(contentStr);
            conn.Open();
            cmd = new SqlCommand("sp_metauser_create", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = "eScience4All!"; // stolen from generic password
            cmd.ExecuteNonQuery();
            conn.Close();

            Console.WriteLine("Successfully added user");
        }
    }
}
